package com.fenghongzhang.day014;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private ServiceConnection serviceConnection;
    private  Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view) {
       intent = new Intent(this, MyService.class);
       startService(intent);
    }

    public void stop(View view) {
        stopService(intent);
    }

    public void bind(View view) {
        Intent intent = new Intent(this,MyService.class);
         serviceConnection = new ServiceConnection() {
            /**
             * 链接的时候调用
             * @param name
             * @param service
             */
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyService.MyBinder binder = (MyService.MyBinder) service;
                binder.callTest();
                binder.callTest2();
            }
            /**
             * 断开的时候调用
             * @param name
             */
            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }

    public void unbind(View view) {
        unbindService(serviceConnection);
    }
}
